package com.shine.app.ui;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.bean.GolfCourseBean;
import com.shine.app.bean.responses.ValidateResponse;

public class ViewMyInfo_AddToFavoriteActivity extends BaseActivity {
	private GolfCourseBean golfCourseBean;
	private AddToFavoriteAsyncTask addToFavoriteAsyncTask = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loding);
		golfCourseBean = (GolfCourseBean) getIntent().getSerializableExtra(
				"golfCourseBean");
		String golferId = appContext.getLoginInfo().getUser().getGolferId();
		addToFavoriteAsyncTask = new AddToFavoriteAsyncTask();
		addToFavoriteAsyncTask.execute(golferId, golfCourseBean);
	}

	public class AddToFavoriteAsyncTask extends
			AsyncTask<Object, Integer, ValidateResponse> {
		private String golferId;
		private GolfCourseBean golfCourseBean;

		@Override
		protected ValidateResponse doInBackground(Object... params) {
			ValidateResponse response = null;
			golferId = (String) params[0];
			golfCourseBean = (GolfCourseBean) params[1];
			response = appContext.addFavoriteCourse(golferId,
					golfCourseBean.getId());
			return response;
		}

		@Override
		protected void onPostExecute(ValidateResponse result) {
			super.onPostExecute(result);
			if (result != null) {
				if (result.isData()) {
					Intent intent = new Intent(
							ViewMyInfo_AddToFavoriteActivity.this,
							ViewMyInfo_AddedFavoriteActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.putExtra("golfCourseBean", golfCourseBean);
					ViewMyInfo_AddToFavoriteActivity.this.startActivity(intent);
				}
			}
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.slide_in_up, R.anim.anim);
		}
	}

	@Override
	protected void onDestroy() {
		if (addToFavoriteAsyncTask != null
				&& addToFavoriteAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			addToFavoriteAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

}
